<template>
  <div>
    <ShopcartHeader />
    <table class="cart-wrap">
      <tr align="center" class="header-tr">
        <td class="col col-check checkbox">
          <input
            type="checkbox"
            name="chk_list"
            class="col col-check"
            v-model="isChecked"
          />
          <span class="span"> 全选</span>
        </td>
        <td class="col col-img">图片</td>
        <td class="col col-name">商品名称</td>
        <td class="col col-price">单价</td>
        <td class="col col-num">数量</td>
        <td class="col col-total">小计</td>
        <td class="col col-action">操作</td>
      </tr>
      <tr
        class="item-box"
        align="center"
        v-for="shopCart in shopCartList"
        :key="shopCart.saleClassid"
      >
        <td>
          <input
            type="checkbox"
            name="chk_list"
            :checked="shopCart.isChecked"
            class="input-check"
            @click="check(shopCart)"
          />
        </td>
        <td>
          <img :src="shopCart.saleImgUrl" alt="" class="img-td" />
        </td>
        <td>
          <span>{{ shopCart.saleName }}</span>
        </td>
        <td>{{ shopCart.salePrice }}</td>
        <td class="num-td">
          <a class="num-a">-</a>
          <input type="text" class="input-num" v-model="shopCart.saleNum" />
          <a class="num-a">+</a>
        </td>
        <td>{{ shopCart.salePrice * shopCart.saleNum }}元</td>
        <td>
          <!-- <i class="iconfonticon-cha icon"></i> -->
          <i class="iconfont icon-cha icon"></i>
        </td>
      </tr>
    </table>
    <div class="foot-div">
      <a class="div-a">继续购物</a>
      <a class="div-right">已选择6件</a>
      <a class="div-center">xxx</a>
      <span class="foot-span"
        >合计<em>{{ total }}</em
        >元</span
      >
      <span class="foot-pay">去结算</span>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import ShopcartHeader from "./components/ShopcartHeader.vue";
export default {
  name: "ShopCart2",
  components: {
    ShopcartHeader,
  },
  data() {
    return {
      // num: 1,
      url: "",
      is: false,
    };
  },
  mounted() {
    this.getShopCartList();
  },
  methods: {
    getShopCartList() {
      this.$store.dispatch("getShopCartList");
    },
    check(shopCart) {
      shopCart.isChecked = !shopCart.isChecked;
    },
    handleCheck(is) {
      this.shopCartList.map((item) => {
        item.isChecked = !is;
      });
    },
  },
  computed: {
    ...mapState({
      shopCartList: (state) => state.shopcart.shopCartList,
    }),
    total() {
      return this.shopCartList.reduce((prve, item, index) => {
        if (item.isChecked) {
          prve += item.saleNum * item.salePrice;
        }
        return prve;
      }, 0);
    },
    isChecked: {
      get() {
        return this.shopCartList.every((item) => item.isChecked);
      },
      set(newValue) {
        this.shopCartList.map((item) => {
          item.isChecked = newValue;
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
.cart-wrap {
  margin: 0 auto;
  width: 1226px;
}
.header-tr {
  font-size: 14px;
}
.col-check {
  margin-left: 20px;
  margin-right: 5px;
  color: #424242;
}
.input-check {
  // padding-right: -20px;
}
.checkbox {
  width: 70px;
  height: 70px;
  display: flex;
  align-items: center;
}
.span {
  font-size: 14px;
  margin: 20px 0;
}
.img-td {
  width: 70px;
  // line-height: 70px;
}
.col-name {
  width: 380px;
  line-height: 70px;
  color: #424242;
}
.col-price {
  width: 140px;
  padding-right: 18px;
  text-align: center;
  color: #424242;
}
.col-num {
  width: 150px;
  text-align: center;
  color: #424242;
  line-height: 70px;
}
.col-total {
  width: 120px;
  padding-right: 81px;
  text-align: right;
  line-height: 70px;
}
.col-action {
  width: 80px;
  text-align: center;
  line-height: 70px;
  color: #424242;
}
.item-box {
  padding: 15px 26px 15px 0;
  border-top: 1px solid #e0e0e0;
  width: 1226px;
  height: 117px;
  font-size: 14px;
  border-bottom: 1px solid #e0e0e0;
}
.icon-checkbox {
  display: inline-block;
  width: 18px;
  height: 18px;
  line-height: 18px;
  border: 1px solid #e0e0e0;
  margin-left: 24px;
  background-color: #fff;
  font-size: 20px;
  text-align: center;
  vertical-align: middle;
  position: relative;
  top: -1px;
  cursor: pointer;
  margin-right: 15px;
}
.input-num {
  float: left;
  width: 72px;
  height: 38px;
  line-height: 38px;
  padding: 0;
  border-width: 0;
  color: #424242;
  font-size: 16px;
  text-align: center;
  border: 1px solid #e0e0e0;
  margin-bottom: 5px;
}
.num-a {
  float: left;
  width: 28px;
  height: 38px;
  line-height: 38px;
  color: #757575;
  font-size: 28px;
  border: 1px solid #e0e0e0;
}
.icon:hover {
  background-color: #e53935;
  border-radius: 12px;
  font-size: 16px;
}

.div-a {
  width: 56px;
  height: 16px;
  color: #757575;
  margin: 0 0 0 32px;
  font-size: 14px;
  line-height: 50px;
}
.div-right {
  margin-left: 16px;
  padding-left: 16px;
  border-left: 1px solid #eee;
  color: #757575;
  line-height: 50px;
}
.foot-span {
  // float: right;
  width: 30px;
  height: 30px;
  color: #ff6700;
}
.foot-pay {
  float: right;
  display: block;
  width: 202px;
  line-height: 50px;
  height: 50px;
  font-size: 18px;
  color: #fff;
  background: #ff6700;
  margin: 0 0 0 50px;
  text-align: center;
}
.div-center {
  display: inline-block;
  width: 650px;
  height: 50px;
  // background: #e0e0e0;
}
em {
  display: inline-block;
  width: 106px;
  height: 33px;
  color: #ff6700;
  font-size: 30px;
}
.foot-div {
  margin: 20px auto;
  width: 1226px;
  height: 50px;
  position: sticky;
  top: 800px;
  box-shadow: 0 -3px 6px rgb(0 0 0 / 10%);
  position: sticky;
  top: 100px;
}
</style>
